$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL +'news/classifieds/list',
        datatype: "json",
        colModel: [
            { label: '分类id', name: 'id', index: "id",  key: true,hidden:true },
            { label: '类别名称', name: 'classifiedsName', width: 45},
            { label: '创建时间', name: 'createTime' }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.result",
            page: "page.pageNo",
            total: "page.totalPage",
            records: "page.total"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#classifieds',
    data:{
        showList: true,
        title:null,
        news:{
            id:null,
            classifiedsName:null,
            createTime:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.news.classifiedsName = null;
            vm.title = "新增资讯分类";
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改资讯分类";
            $.get(baseURL + "news/classifieds/info/"+id, function(r){
                vm.news = r.data;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "news/classifieds/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code === 'success'){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if (vm.news.classifiedsName == null || vm.news.classifiedsName.trim() == ""){
                showTips("分类名称不能为空", '#classifiedsName');
                return
            }
            if (getByteLen(vm.news.classifiedsName)>12){
                showTips("名称不能多于6个字", '#classifiedsName');
                return
            }
            var url = vm.news.id == null ? "news/classifieds/save" : "news/classifieds/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.news),
                success: function(r){
                    if(r.code === 'success'){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.message);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{ page:page }).trigger("reloadGrid");
        }
    }
});